import { BaseDeployPanel } from "../../../equipment/deploy/panel/BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ObjCreate } from "../../../common/ObjCreate";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../../../equipment/deploy/DeployPanelItem";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { MsgManager } from "../../../../manager/MsgManager";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../../equipment/data/EquipmentModuleConst";
import { EquipUpdateParams } from "../../../equipment/data/EquipUpdateParams";
import { ItemData } from "../../../../resource/info/ItemData";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EquipmentUtil } from "../../../common/util/EquipmentUtil";
import { PetEquipForgeConfig } from "../../../../resource/PetEquipForgeConfig";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ItemExInfo } from "../../../../resource/info/ItemExInfo";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { AdvanceResultPanel } from "../../../equipment/popupPanels/AdvanceResultPanel";
import { PopupManager } from "../../../../manager/PopupManager";
import { PetEquipPrefixInfo } from "../../info/PetEquipPrefixInfo";
type int = number;
//class PetEquipPrefixUpgrade
    
    export  class PetEquipPrefixUpgrade extends BaseDeployPanel
    {
        constructor()
        {
            this._useItems = [];
            this.posData = [{"x": 100, "y": 115, "label": Language.getString(60198), "canRemove": true, "noBoarder": true}, {"x": 208, "y": 115, "label": Language.getString(60199), "canRemove": false, "noBoarder": true}, {"x": 256, "y": 115, "label": Language.getString(60199), "canRemove": false, "noBoarder": true}, {"x": 156, "y": 245, "label": Language.getString(60200), "canRemove": false, "noBoarder": false}, {"x": 45, "y": 80, "label":"", "canRemove": false, "noBoarder": false}];
            this._alarmText = Language.getString(60147);
            this.buttonLabel = Language.getString(60201);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc4=null;
            var loc5=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(60202), 0, 66, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            var loc1=new GImageBitmap("EquipMerge_arrow.swf", 118, 127);
            this.addChild(loc1);
            var loc2=new GImageBitmap("equipment_successRate.png");
            loc2.x = 244;
            loc2.y = 63;
            successRateItem.addChild(loc2);
            successRateLabel.x = 263;
            successRateLabel.y = 87;
            addChild(successRateItem);
            var loc3=0;
            while (loc3 < this.posData.length) 
            {
                loc4 = this.posData[loc3];
                (loc5 = new DeployPanelItem(loc3, loc4.noBoarder)).posIndex = loc3;
                loc5.x = loc4["x"];
                loc5.y = loc4["y"];
                loc5.label = loc4["label"];
                if (loc4.canRemove) 
                {
                    loc5.mouseEnabled = true;
                    loc5.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc5);
                allItem.push(loc5);
                ++loc3;
            }
            confirmButton = ObjCreate.createGButton(this.buttonLabel, 140, 305, 68, 26, this, "ButtonSpecial");
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;

            var loc1;
            e = arg1;
            if (!this.checkCanRaise()) 
            {
                return;
            }
            var loc2=e.target;
            switch (loc2) 
            {
                case confirmButton:
                {
                    if (this.checkWillChangeBind()) 
                    {
                        Alert.show(this._alarmText, Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                onConfirm();
                            }
                            return;
                        })
                    }
                    else 
                    {
                        this.onConfirm();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */checkCanRaise(): Boolean
        {
            if (!allItem[0].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60203));
                return false;
            }
            if (!allItem[1].itemData || allItem[1].enabled == false) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61331));
                return false;
            }
            if (allItem[2].itemData && allItem[2].enabled == false) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(61325, ItemsUtil.getItemName(allItem[2].itemData)));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */onConfirm(): void
        {
            var loc3=null;
            var loc1=new Array<any>();
            if (this._useItems && this._useItems.length > 0) 
            {
                var loc4=0;
                var loc5=this._useItems;
                for(loc3 of loc5) 
                {
                    loc1.push(loc3.uid);
                }
            }
            var loc2=new EquipUpdateParams();
            loc2.operationId = this._operationId;
            loc2.equipPosType = allItem[0].itemData.posType;
            loc2.equipUid = allItem[0].itemData.uid;
            loc2.materials = loc1;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc2, false, true));
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            if (allItem[0].itemData) 
            {
                loc1 = this._prefixUpgradeInfo ? this._prefixUpgradeInfo.cost : 0;
                successRate = 100;
                this.showResultEquip();
            }
            else if (!allItem[0].itemData) 
            {
                clearAllItem();
                loc1 = 0;
            }
            if (allItem[2].itemData) 
            {
                allItem[2].visible = true;
            }
            else 
            {
                allItem[2].visible = false;
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1 && arg1.itemInfo.item.category == ECategory._ECategoryPetEquip) 
            {
                if (EquipmentUtil.isPetEquipHighestPrefix(arg1)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60205));
                    return;
                }
                clearAllItem();
                allItem[0].itemData = arg1;
                this._prefixUpgradeInfo = PetEquipForgeConfig.instance.getPrefixUpgateInfo(arg1.itemExInfo.prefix);
                allItem[4].itemData = this.getBestPreview(arg1);
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [allItem[0].itemData], false, true));
            }
            this.updateStatus();
            return;
        }

        /* internal  */getBestPreview(arg1: ItemData): ItemData
        {
            var loc1=ItemsUtil.duplicateItemData(arg1);
            loc1.itemExInfo.prefix = EquipmentUtil.petEquipHighestPrefix;
            return loc1;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc2=null;
            this._useItems = [];
            var loc1=arg1.materials as Array<any>;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2 = loc1[loc3].material as Array<any>;
                this._useItems = this._useItems.concat(loc2);
                allItem[1 + loc3].itemData = loc2[0];
                allItem[1 + loc3].amount = loc1[loc3].amountNeed;
                allItem[1 + loc3].enabled = loc1[loc3].isEnough;
                ++loc3;
            }
            this.updateStatus();
            return;
        }

        /* internal  */checkWillChangeBind(): Boolean
        {
            var loc1=0;
            if (allItem[0].itemData && !(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes)) 
            {
                loc1 = 0;
                while (loc1 < this._useItems.length) 
                {
                    if (this._useItems[loc1].itemInfo.item.bind == EBind._EBindYes) 
                    {
                        return true;
                    }
                    ++loc1;
                }
            }
            return false;
        }

        /* internal  */showResultEquip(): void
        {
            if (!allItem[0].itemData) 
            {
                allItem[3].itemData = null;
                return;
            }
            var loc1=new ItemData(allItem[0].itemData.itemCode);
            var loc2=new ItemExInfo();
            loc2 = allItem[0].itemData.itemExInfo.duplicate();
            loc2.prefix = loc2.prefix + 1;
            if (this.checkWillChangeBind()) 
            {
                loc2.bind = EBind._EBindYes;
            }
            loc1.itemExInfo = loc2;
            allItem[3].itemData = loc1;
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 == 0) 
            {
                allItem[arg1].itemData = null;
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            }
            return;
        }

        public /* override */ clear(): void
        {
            this._useItems = [];
            this._prefixUpgradeInfo = null;
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60207));
            }
            else 
            {
                loc1 = new ItemData(arg3);
                if (EquipmentUtil.isPetEquipHighestPrefix(loc1)) 
                {
                    (loc2 = new AdvanceResultPanel()).setResultItem(loc1, this._operationId);
                    PopupManager.addPopUp(loc2, "", true);
                    this.clear();
                }
                else 
                {
                    this.putInItem(loc1);
                }
                MsgManager.showRollTipsMsg(Language.getString(60206));
                addUpdateSuccessEffect("equipEffect_qualityUp.swf", 175, 216);
            }
            return;
        }

        /* internal */ /* var */_operationId: int=21;

        /* internal */ /* var */_prefixUpgradeInfo: PetEquipPrefixInfo;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */buttonLabel: string;
    }
